/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.bannersample.util.NumberUtil;
import ohos.agp.components.Component;

/**
 * ScaleInOutTransformer
 *
 * @since 2021-08-06
 */
public class ScaleInOutTransformer implements BannerViewPager.PageTransformer {
    private static final float NUM_F_2 = 2.0f;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float scale = 0F;

        switch (pageType) {
            case PREVIOUS:
                float previousFloat = 1 - offset;
                if (offsetPixels < 0) {
                    page.setPivotX(page.getWidth());
                    page.setPivotY((float) page.getHeight() / NUM_F_2);
                    scale = NumberUtil.sub(1.0F, previousFloat);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                }
                break;
            case CURRENT:
                if (offsetPixels >= 0) {
                    page.setPivotX(page.getWidth());
                    page.setPivotY((float) page.getHeight() / NUM_F_2);
                    scale = NumberUtil.sub(1.0F, offset);
                } else {
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / NUM_F_2);
                    scale = NumberUtil.plus(1.0F, offset);
                }
                page.setScaleX(scale);
                page.setScaleY(scale);
                break;

            case NEXT:
                float nextFloat = offset - 1;
                if (offsetPixels >= 0) {
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / NUM_F_2);
                    scale = NumberUtil.plus(1.0F, nextFloat);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                } else {
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / NUM_F_2);
                    scale = NumberUtil.plus(1.0F, nextFloat);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                }
                break;

            case OTHER:
                other(page);
                break;
            default:
                break;
        }
    }

    private void other(Component page) {
        page.setPivotX(1F);
        page.setPivotY(1F);
        page.setScaleX(1F);
        page.setScaleY(1F);
    }
}
